Skip to content

fix: preserve riscv trap pc - #2384

Open
Jvlegod wants to merge 1 commit into
unicorn-engine:devfrom
Jvlegod:fix-riscv-intr-pc
Open

fix: preserve riscv trap pc#2384
Jvlegod wants to merge 1 commit into
unicorn-engine:devfrom
Jvlegod:fix-riscv-intr-pc

Conversation

@Jvlegod

@Jvlegod Jvlegod commented Aug 9, 2026

Copy link
Copy Markdown

This PR fixes RISC-V exception PC handling in interrupt hooks.

Previously, Unicorn unconditionally advanced the RISC-V PC by 4 before invoking UC_HOOK_INTR. That behavior was added to solve ecall resume problem reported in #1477, but it also made interrupt hooks observe the wrong PC for other exceptions, as reported in #1990.

This change preserves the faulting instruction address while the interrupt hook is running, and only advances PC to the saved next PC after a handled RISC-V ecall.

Note: ebreak will follow the UC_ERR_INSN_INVALID path is not affected by this change.

Signed-off-by: Jvle <keke.oerv@isrc.iscas.ac.cn>
@Kreijstal

Kreijstal commented Aug 20, 2026

Copy link
Copy Markdown

I closed #2400 in favor of this PR.

One part from it is still useful. For instruction, load, and store address faults, RISC-V places the faulting address in mtval. UC_HOOK_INTR should be able to read that value.

I split the fix into a commit based directly on this PR:

Kreijstal@c3cb167

It does not change this PR's PC handling. I built it with UNICORN_ARCH=riscv, and all 30 RISC-V tests pass. Feel free to take the commit, or I can submit it separately after this PR lands.

@Jvlegod

Jvlegod commented Aug 20, 2026

Copy link
Copy Markdown
Author

Hello @wtdcode.

#2400 was closed in favor of this PR, @Kreijstal extracted the remaining change into c3cb167. I have verified that it applies cleanly on top of this branch. If you think this PR is suitable, I will cherry-pick the new code.

:>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants